package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.AppManger;
import com.android.frame.util.DateTool;
import com.android.frame.util.Toolkit;
import com.android.frame.view.dialog.NiftyDialogBuilder;
import com.android.frame.view.linearlistview.LinearListView;
import com.bigkoo.pickerview.TimePickerView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.JobPushAdapter;
import com.kupurui.asteacher.bean.PushClassInfo;
import com.kupurui.asteacher.bean.PushSuccessInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 发布作业
 * Created by Administrator on 2017/7/25.
 */

public class JobPushAty extends BaseAty {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.listview)
    LinearListView listview;
    @Bind(R.id.fbtn_push)
    FButton fbtnPush;
    @Bind(R.id.ll_push_time)
    LinearLayout llPushTime;
    @Bind(R.id.tv_push_time)
    TextView tvPushTime;
    @Bind(R.id.ll_end_time)
    LinearLayout llEndTime;
    @Bind(R.id.tv_end_time)
    TextView tvEndTime;
    @Bind(R.id.et_homework_title)
    EditText etHomeworkTitle;

    JobPushAdapter adapter;
    List<PushClassInfo.SchoolBean> list;
    PushClassInfo pushClassInfo;

    NiftyDialogBuilder dialogBuilder;

    PushSuccessInfo pushSuccessInfo;
    List<String> question;
    List<String> class_id;

    private String title = "";
    private String issue_time = "立即发布";
    private String cut_time = "";
    private String iss_date = "立即发布";
    private String cut_date = "";
    private String class_name = "";

    NiftyDialogBuilder pushDialog;

    @Override
    public int getLayoutId() {
        return R.layout.job_push_aty;
    }

    @Override
    public void initData() {
        question = new ArrayList<>();
        class_id = new ArrayList<>();
        if (getIntent().getExtras() != null) {
            question = getIntent().getStringArrayListExtra("id");
        }
        list = new ArrayList<>();
        adapter = new JobPushAdapter(this, list, R.layout.job_push_item);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new LinearListView.OnItemClickListener() {
            @Override
            public void onItemClick(LinearListView parent, View view, int position, long id) {
                if (list.get(position).is_sel()) {
                    list.get(position).setIs_sel(false);
                } else {
                    list.get(position).setIs_sel(true);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().zuoye(UserManger.getId(), this, 0);
    }

    @OnClick({R.id.ll_push_time, R.id.ll_end_time, R.id.fbtn_push, R.id.iv_back})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_push_time://发布时间
                TimePickerView timePickerView = new TimePickerView(this, TimePickerView.Type.ALL);
                timePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date) {
                        tvPushTime.setText(DateTool.dateToStr(date, "yyyy年MM月dd日 HH:mm"));
                        issue_time = DateTool.dateToStr(date,"yyyy-MM-dd HH:mm");
                        iss_date = DateTool.dateToStr(date,"yyyy年MM月dd日 HH:mm");
                    }
                });
                timePickerView.show();
                break;
            case R.id.ll_end_time://截止时间
                TimePickerView timePickerView1 = new TimePickerView(this, TimePickerView.Type.ALL);
                timePickerView1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date) {
                        tvEndTime.setText(DateTool.dateToStr(date, "yyyy年MM月dd日 HH:mm"));
                        cut_time = DateTool.dateToStr(date,"yyyy-MM-dd HH:mm");
                        cut_date = DateTool.dateToStr(date,"yyyy年MM月dd日 HH:mm");
                    }
                });
                timePickerView1.show();
                break;
            case R.id.fbtn_push:
                toReq();
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what) {
            case 0:
                pushClassInfo = AppJsonUtil.getObject(result,PushClassInfo.class);
                list.clear();
                list.addAll(pushClassInfo.getSchool());
                adapter.notifyDataSetChanged();
                break;
            case 1:
                UserManger.subject_id.clear();
                pushSuccessInfo = AppJsonUtil.getObject(result,PushSuccessInfo.class);
                AppManger.getInstance().killActivity(PreviewAty.class);
                AppManger.getInstance().killActivity(JobSelAty.class);
                AppManger.getInstance().killActivity(SelKnowledgeAty.class);
                AppManger.getInstance().killActivity(JobArrangementAty.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean",pushSuccessInfo);
                startActivity(JobPushSuccessAty.class, bundle);
                dialogBuilder.dismiss();
                finish();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if (dialogBuilder != null){
            dialogBuilder.dismiss();
        }
        super.onError(result, call, response, what);
    }

    @Override
    public void onFailure(Call call, int what) {
        if (dialogBuilder != null){
            dialogBuilder.dismiss();
        }
        super.onFailure(call, what);
    }

    private void showDialog() {
        dialogBuilder = new NiftyDialogBuilder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.job_push_dialog, null);
        dialogBuilder.setND_AddCustomView(view);

        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tvSubmit = (TextView) view.findViewById(R.id.tv_submit);
        TextView tvPushTime = (TextView) view.findViewById(R.id.tv_push_time);
        TextView tvCusTime = (TextView) view.findViewById(R.id.tv_cus_time);
        TextView tvClassName = (TextView) view.findViewById(R.id.tv_class_name);

        tvPushTime.setText("发布时间："+iss_date);
        tvCusTime.setText("截至时间："+cut_date);
        tvClassName.setText("发布班级："+class_name);


        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消
                dialogBuilder.dismiss();
            }
        });
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确认
                showLoadingDialog("");
                new Teacher().publish(UserManger.getId(),title,issue_time,cut_time,class_id,question,JobPushAty.this,1);
            }
        });

        dialogBuilder.show();
    }

    /**
     * 发布作业
     */
    private void toReq() {
        title = etHomeworkTitle.getText().toString();
        class_id.clear();
        class_name = "";
        for (int i = 0;i<list.size();i++){
            if (list.get(i).is_sel()){
                class_id.add(list.get(i).getId());
                if (TextUtils.isEmpty(class_name)){
                    class_name = list.get(i).getClass_name();
                }else{
                    class_name = class_name+"，"+list.get(i).getClass_name();
                }
            }
        }
        if (TextUtils.isEmpty(title)){
            showToast("请输入作业名称");
            return;
        }
        if (Toolkit.listIsEmpty(class_id)){
            showToast("请选择发布的班级");
            return;
        }
        if (TextUtils.isEmpty(cut_time)){
            showToast("请选择截至时间");
            return;
        }
        showDialog();
    }

}
